package cn.com.open.itembank.mapper;

import cn.com.open.itembank.entity.TestPaper;
import cn.com.open.itembank.entity.TestPaperContent;
import org.apache.ibatis.annotations.*;

/**
 * Created by lucky on 2017/10/30.
 */
@Mapper
public interface PaperWriteMapper {

    //region 试卷状态更改

    /**
     * 试卷状态更改
     *
     * @param id       试卷id
     * @param newState 试卷状态
     * @return the int
     */
    @Update("UPDATE TestPaper SET State=${state} WHERE TestPaperID=#{id}")
    int updateStatus(@Param("id") String id, @Param("state") int newState);

    //endregion

    //region 试卷名称更改

    /**
     * 试卷名称更改
     *
     * @param id            试卷id
     * @param testPaperName 试卷名称
     * @return the int
     */
    @Update("UPDATE TestPaper SET TestPaperName=#{testPaperName} WHERE TestPaperID=#{id}")
    void updateTestPaperName(@Param("id") String id, @Param("testPaperName") String testPaperName);

    //endregion

    //region 发布试卷

    /**
     * 更改曝光信息表的曝光时间.
     *
     * @param id          试卷id
     * @param questionId  试题id
     * @param displayTime 曝光时间
     * @return the int
     */
    @Update("UPDATE QuestionDisplayInfo SET DisplayTime=#{displayTime} WHERE TestPaperID=#{id} AND QuestionID=#{questionId}")
    void updateQuestionDisplayInfo(@Param("id") String id, @Param("questionId") String questionId, @Param("displayTime") String displayTime);

    /**
     * ####更改当前试卷所有试题的曝光次数，最后曝光时间.
     *
     * @param questionId   试题id
     * @param displayCount 曝光次数
     * @param displayTime  曝光时间
     * @return the int
     */
    @Update("UPDATE Question SET DisplayCount=${displayCount} , DisplayTime=#{displayTime} WHERE QuestionID=#{questionId}")
    void updateQuestionDisplayTime(@Param("questionId") String questionId, @Param("displayCount") int displayCount, @Param("displayTime") String displayTime);

    /**
     * 更改当前试卷的发布人，发布状态.
     *
     * @param isPublish   发布状态
     * @param publisher   发布人
     * @param publishTime 发布时间
     * @param id          试卷ID
     * @return the int
     */
    @Update("UPDATE TestPaper SET IsPublish = ${isPublish}, Publisher=#{publisher} , PublishTime = #{publishTime} " +
            "WHERE TestPaperID=#{id}")
    void updateIsPublish(@Param("isPublish") int isPublish, @Param("publisher") String publisher,
                         @Param("publishTime") String publishTime, @Param("id") String id);

    //endregion

    //region 取消试卷发布

    /**
     * 更改当前试卷的发布人，发布状态.
     *
     * @param isPublish   发布状态
     * @param publisher   发布人
     * @param publishTime 发布时间
     * @param id          试卷ID
     * @return the int
     */
    @Update("UPDATE TestPaper SET IsPublish=${isPublish}, Publisher = #{publisher}, PublishTime=#{publishTime} WHERE TestPaperID=#{id}")
    int updateStopPublisher(@Param("isPublish") int isPublish, @Param("publisher") String publisher, @Param("publishTime") String publishTime, @Param("id") String id);

    //endregion

    /**
     * 增加试卷内容表
     *
     * @param content the content
     * @return int
     */
    @Insert("INSERT INTO TestPaperContent (TestPaperID,ContentXml,ContentJson) VALUES (#{testPaperID},#{contentXml},'')")
    int insertTestPaperContent(TestPaperContent content);

    /**
     * 增加试卷表.
     *
     * @param testPaper the test paper
     * @return the int
     */
    //@InsertProvider(type = TestPaperWriteSqlProvider.class, method = "insert")
    @Insert("INSERT INTO TestPaper (TestPaperID,TestPaperName,CourseID,Mode,RuleID,State,Score,DisplayCount,DisplayTime,Comment,Creater,CreateTime,IsCheck,Checker,CheckTime,Source,Modifier,ModifyTime,IsPublish,Publisher,PublishTime,TestPaperVersion,OrganizationID) VALUES (#{testPaperID},#{testPaperName},#{courseID},#{mode},#{ruleID},#{state},#{score},#{displayCount},#{displayTime},#{comment},#{creater},#{createTime},#{isCheck},#{checker},#{checkTime},#{source},#{modifier},#{modifyTime},#{isPublish},#{publisher},#{publishTime},#{testPaperVersion},#{organizationID})")
    int insertTestPaperModel(TestPaper testPaper);

    /**
     * 新增试题曝光表信息
     *  @param id          试卷id
     * @param questionId  试题id
     * @param displayTime 曝光时间
     */
    @Insert("INSERT INTO QuestionDisplayInfo (TestPaperID,QuestionID,DisplayTime) VALUES (#{id},#{questionId},#{displayTime})")
    void insertQuestionDisplayInfo(@Param("id") String id, @Param("questionId") String questionId, @Param("displayTime") String displayTime);

    /**
     * 删除试题曝光表信息
     *
     * @param questionId 试题id
     * @return boolean
     */
    @Delete("DELETE FROM QuestionDisplayInfo WHERE QuestionID=#{questionId}")
    boolean deleteQuestionDisplayInfo(@Param("questionId") String questionId);

    /**
     * 更新试卷xml
     *
     * @param xml 试卷xml
     * @param id  试卷id
     * @return int
     */
    @Update("UPDATE TestPaperContent SET ContentXml=#{contentXml} WHERE TestPaperID=#{id}")
    int updateTestPaperContent(@Param("contentXml") String xml, @Param("id") String id);

    /**
     * 更新试卷分数
     *
     * @param score 试卷分数
     * @param id    试卷id
     * @return int
     */
    @Update("UPDATE TestPaper SET Score=#{score} WHERE TestPaperID=#{id}")
    int updateTestPaperScore(@Param("score") String score, @Param("id") String id);

    /**
     * 更新试卷json
     *  @param jsonString 试卷json信息
     * @param id         试卷id
     */
    @Update("UPDATE TestPaperContent SET ContentJSON=#{jsonString} WHERE TestPaperID=#{id}")
    boolean updateContentJSON(@Param("jsonString") String jsonString, @Param("id") String id);

    /**
     * 删除曝光信息表
     * @param id
     * @return
     */
    @Delete("DELETE FROM QuestionDisplayInfo WHERE TestPaperID=#{id}")
    int deletePaperDisplay(@Param("id") String id);
}
